<?php if (!defined('THINK_PATH')) exit();?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> ThinkAM 开源的mvc生成工具 by thinkphp</title>
<link rel="stylesheet" type="text/css" href="../Public/css/<?php echo ((C("web_css_style"))?(C("web_css_style")):'blue'); ?>.css" />
<script type="text/javascript" src="../Public/js/common.js"></script>
<script type='text/javascript' src='/thinkam/Public/Js/Base.js'></script>  
<script type='text/javascript' src='/thinkam/Public/Js/prototype.js'></script>  
<script type='text/javascript' src='/thinkam/Public/Js/mootools.js'></script>  
<script type='text/javascript' src='/thinkam/Public/Js/Think/ThinkAjax.js'></script>  
<script type='text/javascript' src='/thinkam/Public/Js/Form/CheckForm.js'></script>  
<script type='text/javascript' src='/thinkam/Public/Js/Util/ImageLoader.js'></script>  
<script language="JavaScript">
<!--
//指定当前组模块URL地址 
var URL = '__URL__';
var APP	 =	 '__APP__';
var PUBLIC = '__PUBLIC__';
ThinkAjax.image = [	 '../Public/images/loading2.gif', '../Public/images/ok.gif','../Public/images/update.gif' ]
ImageLoader.add("../Public/images/bgline.gif","../Public/images/bgcolor.gif","../Public/images/titlebg.gif");
ImageLoader.startLoad();
//-->
</script>
</head>

<body onload="loadBar(0)">
<div id="loader" >页面加载中...</div>
<div id="header" class="header">
<div class="headTitle" style="margin:8pt 10pt"> ThinkAM 开源的mvc生成工具 </div>
</div>
<script language="JavaScript">
<!--
	function buildTable(tableId,data) { 
	for (i=0;i<data.length;i++){ 
	  var row =$(tableId).insertRow(-1);
	  if (i==0)
	  {row.className = 'over';}
	  for (j=0;j<data[i].length;j++) { 
		var cell = document.createElement("td");
		cell.innerHTML = data[i][j];
		row.appendChild(cell); 
		//var cell=row.insertCell(-1);
		//cell.innerText=data[i][j];
	   }
	 }
	 }
	function deleteTable(tableId){
		var Table	=	$(tableId);
			while (Table.rows.length > 0) {
				Table.deleteRow(0);
			}
	}

	function handleSql(data){
		document.getElementById('resultNum').innerHTML=	 '';
		deleteTable('table1');
		deleteTable('table2');
		var time = data.splice(0,1);
		var bench = data.splice(0,1);
		if (bench[0])
		{		
			document.getElementById('benchbox').style.display = 'block';
			buildTable('table2',bench[0]);
		}else{
			document.getElementById('benchbox').style.display = 'none';
		}
		if(data.length<=1) {
			document.getElementById('resultNum').innerHTML	=	'[ 返回0条查询记录 耗时:'+time+' ] ';
		}else {
			document.getElementById('resultNum').innerHTML	=	'[ 返回'+(data.length-1)+'条查询记录 耗时:'+time+' ] ';
			buildTable('table1',data);
		}
	}
	function runSql(){
		ThinkAjax.send('__URL__/execute','ajax=1&sql='+document.getElementById('execSql').value,handleSql);
	}

	function executeSql(sql){
		var tableName	=	$F('tableName');	
		if (tableName == '')
		{
		alert('没有选择数据表');return ;
		}
		sql	 =	 sql.replace('$table',tableName);
		if ($F('explain'))
		{
			sql = 'EXPLAIN '+sql;
		}
		document.getElementById('execSql').value	=	sql;
		bench	 =	 $F('bench');
		if (undefined == bench)
		{bench = 0;}
		record	 =	 $F('record');
		if (undefined == record)
		{record = 0;}
		ThinkAjax.send('__URL__/execute','ajax=1&record='+record+'&bench='+bench+'&sql='+sql,handleSql);
	}
	function showTable(){
			if ($F('explain'))
		{alert('只有查询才支持EXPALIN');return ;}
		executeSql('SHOW COLUMNS FROM $table');
	}
	function clearTable(){
		var tableName	=	$F('tableName');	
		if (tableName == '')
		{
		alert('没有选择数据表');return ;
		}
		if (window.confirm('确实清空表'+tableName+'吗？'))
		{
		executeSql('TRUNCATE TABLE $table');
		}
	}
	function checkTable(){
		if ($F('explain'))
		{alert('只有查询才支持EXPALIN');return ;}
		executeSql('CHECK TABLE $table');
	}
	function searchTable(){
		executeSql('SELECT * FROM $table LIMIT 0,30');
	}
	function optimzeTable(){
		if ($F('explain'))
		{alert('只有查询才支持EXPALIN');return ;}
		executeSql('OPTIMIZE TABLE $table');
	}
	function analyzeTable(){
		if ($F('explain'))
		{alert('只有查询才支持EXPALIN');return ;}
		executeSql('ANALYZE TABLE $table');
	}
	function repairTable(){
		if ($F('explain'))
		{alert('只有查询才支持EXPALIN');return ;}
		executeSql('REPAIR TABLE $table');
	}
	function statusTable(){
		if ($F('explain'))
		{alert('只有查询才支持EXPALIN');return ;}
		executeSql('SHOW TABLE STATUS LIKE  \'$table\'');
	}
	function delTable(){
		var tableName	=	$F('tableName');	
		if (tableName == '')
		{
		alert('没有选择数据表');return ;
		}
		if (window.confirm('确实删除表'+tableName+'吗？'))
		{
		executeSql('DROP TABLE  $table');
		}
	}
	function outputTable(){
		var tableName	=	$F('tableName');
		window.location.href = '__URL__/output/table/'+tableName;
	}
	function importSql(){
		window.location.href = '__URL__/import';
	}
	function refleshDb(db){
		document.getElementById('useDb').innerHTML = db;
		ThinkAjax.send('__URL__/getTables','ajax=1&db='+db,fleshTable);
	}
	function fleshTable(data,status){
		if (status==1)
		{
			document.getElementById('tableName').options.length = 0;
			// 动态增加
			for (j=0;j<data.length;j++) 
			{
			document.getElementById('tableName').options.add( new Option(data[j],data[j]) );
			}
		}

	}
//-->
</script>
<!-- 菜单区域  -->

<!-- 主页面开始 -->
<div id="main" class="main" >
<!-- 主体内容  -->
<div class="content" >
<div class="title">首页 <span style="font-weight:normal;">[当前数据库:<span style="color:blue" id="useDb"><?php echo ($_SESSION['useDb']); ?></span>]</span></div>
<div class="operate" >
<div class="impBtn hMargin fLeft shadow" ><input type="button" id="" name="add" value="建库" onclick="window.location.href='__URL__/addDb'" class="add imgButton"></div>
<div class="impBtn hMargin fLeft shadow" ><input type="button" id="" name="add" value="建表" onclick="add()" class="add imgButton"></div>
<div class="impBtn hMargin fLeft shadow" ><input type="button" id="" name="ok" value="导入" onclick="importSql()" class="ok imgButton"></div>
<div class="impBtn hMargin fLeft shadow" ><input type="button" id="" name="sort" value="导出" onclick="outputTable()" class="sort imgButton"></div>
<div class="impBtn hMargin fLeft shadow" ><input type="button" id="" name="clear" value="清空" onclick="clearTable()" class="clear imgButton"></div>
<div class="impBtn hMargin fLeft shadow" ><input type="button" id="" name="delete" value="删除" onclick="delTable()" class="delete imgButton"></div>
<div class="impBtn hMargin fLeft shadow" ><input type="button" id="" name="adv" value="高级" onclick="window.location.href='__URL__/adv'" class="adv imgButton"></div>
</div>
<!--  功能操作区域  -->
<div class="cBoth">
<!-- SQL执行器 -->
<form method=post action="">
	<fieldset id="querybox" >
<legend style="color:gray">
<div class="fLeft">在数据库</div>
<select name="database" class="fLeft hMargin" onchange="refleshDb(this.options[this.options.selectedIndex].value)">
<?php if(is_array($dbs)): $i = 0; $__LIST__ = $dbs;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$db): ++$i;$mod = ($i % 2 )?><?php if(($db["Database"])  !=  "information_schema"): ?><option value="<?php echo ($db["Database"]); ?>" <?php if(($db["Database"])  ==  $useDb): ?>selected<?php endif; ?>> <?php echo ($db["Database"]); ?> </option><?php endif; ?><?php endforeach; endif; else: echo "" ;endif; ?>
   </select>
<div class="fLeft">中执行SQL语句（<span style="color:orange">请慎用</span>） <input type="checkbox" name="bench" value="1"> 性能分析 <input type="checkbox" name="explain" value="1"> 查询解释 <input type="checkbox" name="record" value="1"> 记录SQL</div></legend>
<div class="cBoth"></div>
<div id="dbSql" class="cBoth vMargin">
<div class="fLeft"><TEXTAREA id="execSql" NAME="sql" check="Require" warning="输入要执行的SQL语句！" style="width:480px;height:200px;overflow:auto;line-height:155%" ><?php echo ($_GET['sql']); ?></TEXTAREA></div>
<div id="dbSql" >
<div class="fLeft hMargin">
<select NAME="tableName" id="tableName" class="medium" size="12" multiple >
<?php if(is_array($tables)): $i = 0; $__LIST__ = $tables;if( count($__LIST__)==0 ) : echo "" ;else: foreach($__LIST__ as $key=>$table): ++$i;$mod = ($i % 2 )?><option value="<?php echo ($table); ?>"><?php echo ($table); ?></option><?php endforeach; endif; else: echo "" ;endif; ?>
</select></div>
<div class="fLeft" style="width:65px">
<div class=" fLeft shadow" ><input type="button" id=""  name="" value="查询" onclick="searchTable()" class="button"></div>
<div class=" fLeft shadow" ><input type="button" id=""  name="" value="结构" onclick="showTable()" class="button"></div>
<div class="  fLeft shadow" ><input type="button" id=""  name="" value="优化" onclick="optimzeTable()" class="button"></div>
<div class="  fLeft shadow" ><input type="button" id=""  name="" value="检查" onclick="checkTable()" class="button"></div>
<div class="  fLeft shadow" ><input type="button" id=""  name="" value="分析" onclick="analyzeTable()" class="button"></div>
<div class="  fLeft shadow" ><input type="button" id=""  name="" value="修复" onclick="repairTable()" class="button"></div>
<div class="  fLeft shadow" ><input type="button" id=""  name="" value="状态" onclick="statusTable()" class="button"></div>
</div>
</div>
</div>
<br class="cBoth">
<div class="cBoth vMargin" >
<div class="impBtn hMargin fLeft shadow" ><input type="button" id="" name="ok" value="执行" onclick="runSql()" class="ok imgButton"></div>
<div class="impBtn hMargin fLeft shadow" ><input type="reset" id="" name="sort" value="重置" onclick="" class="sort imgButton"></div>
</div>
</fieldset>
</form>

<!-- 表操作器 -->
<fieldset id="benchbox" style="display:none">
<legend style="color:gray">数据库性能分析 </legend>
<div id="SqlBench" style="overflow:auto;width:100%;height:100%">
<table id='table2' class="list" style="width:450px"></table>
</div>
</fieldset>
<fieldset id="querybox" >
<legend style="color:gray">数据库查询结果 <span class="blue" id="resultNum"></span> </legend>
<div id="SqlResult" style="overflow:auto;width:100%;height:100%">
<table id='table1' class="list" ></table>
</div>
</fieldset>
</div>
<!-- 列表显示区域结束 -->
</div>
<!-- 主体内容结束 -->
</div>
<!-- 主页面结束 -->
<script language="JavaScript">
<!--
	if (<?php echo (($_GET['run'])?($_GET['run']):0); ?>)	runSql();
//-->
</script>
 <style>
 div.footer{ clear:both; padding:8px 0px; width:100%; text-align:center; font:normal normal normal 12px Verdana,Geneva,Arial,Helvetica,sans-serif; background-color:#464646; border-top:2px solid silver; color:silver}
div.footer a{color:white; text-decoration:none; border-bottom:1px dotted}
div.footer a:hover{color:silver; text-decoration:none; border-bottom:1px dotted}
.think_run_time{text-align:center; width:100%;font-size:12px;}
</style>
{__RUNTIME__}
<!-- 版权信息区域 -->
<div id="footer" class="footer" >Powered by ThinkPHP <?php echo (THINK_VERSION); ?> | Template designed by <a target="_blank" href="http://thinkphp.cn">ThinkPHP</a> <span id="run"></span>
</div>
</body>
</html>